home *** CD-ROM | disk | FTP | other *** search
-
-
-
- TTTThhhhrrrreeeeaaaadddd((((3333)))) 22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222)))) TTTThhhhrrrreeeeaaaadddd((((3333))))
-
-
-
- NNNNAAAAMMMMEEEE
- Thread - multithreading
-
- SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
- use Thread;
-
- my $t = new Thread \&start_sub, @start_args;
-
- $t->join;
-
- my $tid = Thread->self->tid;
-
- my $tlist = Thread->list;
-
- lock($scalar);
-
- use Thread 'async';
-
- use Thread 'eval';
-
-
- DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
- The Thread module provides multithreading support for perl.
-
- FFFFUUUUNNNNCCCCTTTTIIIIOOOONNNNSSSS
- new \&start_sub
-
- new \&start_sub, LIST
- new starts a new thread of execution in the
- referenced subroutine. The optional list is passed
- as parameters to the subroutine. Execution continues
- in both the subroutine and the code after the new
- call.
-
- new Thread returns a thread object representing the
- newly created thread.
-
- lock VARIABLE
- lock places a lock on a variable until the lock goes
- out of scope. If the variable is locked by another
- thread, the lock call will block until it's
- available. lock is recursive, so multiple calls to
- lock are safe--the variable will remain locked until
- the outermost lock on the variable goes out of
- scope.
-
- Locks on variables only affect lock calls--they do
- _n_o_t affect normal access to a variable. (Locks on
- subs are different, and covered in a bit) If you
- really, _r_e_a_l_l_y want locks to block access, then go
- ahead and tie them to something and manage this
- yourself. This is done on purpose. While managing
-
-
-
- Page 1 (printed 10/23/98)
-
-
-
-
-
-
- TTTThhhhrrrreeeeaaaadddd((((3333)))) 22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222)))) TTTThhhhrrrreeeeaaaadddd((((3333))))
-
-
-
- access to variables is a good thing, perl doesn't
- force you out of its living room...
-
- If a container object, such as a hash or array, is
- locked, all the elements of that container are not
- locked. For example, if a thread does a lock @a, any
- other thread doing a lock($a[12]) won't block.
-
- You may also lock a sub, using lock &sub. Any calls
- to that sub from another thread will block until the
- lock is released. This behaviour is not equvalent to
- use attrs qw(locked) in the sub. use attrs
- qw(locked) serializes access to a subroutine, but
- allows different threads non-simultaneous access.
- lock &sub, on the other hand, will not allow _a_n_y
- other thread access for the duration of the lock.
-
- Finally, lock will traverse up references exactly
- _o_n_e level. lock(\$a) is equivalent to lock($a),
- while lock(\\$a) is not.
-
- async BLOCK;
- async creates a thread to execute the block
- immediately following it. This block is treated as
- an anonymous sub, and so must have a semi-colon
- after the closing brace. Like new Thread, async
- returns a thread object.
-
- Thread->self
- The Thread->self function returns a thread object
- that represents the thread making the Thread->self
- call.
-
- Thread->list
- Thread->list returns a list of thread objects for
- all running and finished but un-joined threads.
-
- cond_wait VARIABLE
- The cond_wait function takes a lllloooocccckkkkeeeedddd variable as a
- parameter, unlocks the variable, and blocks until
- another thread does a cond_signal or cond_broadcast
- for that same locked variable. The variable that
- cond_wait blocked on is relocked after the cond_wait
- is satisfied. If there are multiple threads
- cond_waiting on the same variable, all but one will
- reblock waiting to reaquire the lock on the
- variable. (So if you're only using cond_wait for
- synchronization, give up the lock as soon as
- possible)
-
- cond_signal VARIABLE
- The cond_signal function takes a locked variable as
-
-
-
- Page 2 (printed 10/23/98)
-
-
-
-
-
-
- TTTThhhhrrrreeeeaaaadddd((((3333)))) 22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222)))) TTTThhhhrrrreeeeaaaadddd((((3333))))
-
-
-
- a parameter and unblocks one thread that's
- cond_waiting on that variable. If more than one
- thread is blocked in a cond_wait on that variable,
- only one (and which one is indeterminate) will be
- unblocked.
-
- If there are no threads blocked in a cond_wait on
- the variable, the signal is discarded.
-
- cond_broadcast VARIABLE
- The cond_broadcast function works similarly to
- cond_wait. cond_broadcast, though, will unblock aaaallllllll
- the threads that are blocked in a cond_wait on the
- locked variable, rather than only one.
-
- MMMMEEEETTTTHHHHOOOODDDDSSSS
- join join waits for a thread to end and returns any
- values the thread exited with. join will block until
- the thread has ended, though it won't block if the
- thread has already terminated.
-
- If the thread being joined died, the error it died
- with will be returned at this time. If you don't
- want the thread performing the join to die as well,
- you should either wrap the join in an eval or use
- the eval thread method instead of join.
-
- eval The eval method wraps an eval around a join, and so
- waits for a thread to exit, passing along any values
- the thread might have returned. Errors, of course,
- get placed into $@.
-
- tid The tid method returns the tid of a thread. The tid
- is a monotonically increasing integer assigned when
- a thread is created. The main thread of a program
- will have a tid of zero, while subsequent threads
- will have tids assigned starting with one.
-
- LLLLIIIIMMMMIIIITTTTAAAATTTTIIIIOOOONNNNSSSS
- The sequence number used to assign tids is a simple integer,
- and no checking is done to make sure the tid isn't currently
- in use. If a program creates more than 2^32 - 1 threads in a
- single run, threads may be assigned duplicate tids. This
- limitation may be lifted in a future version of Perl.
-
- SSSSEEEEEEEE AAAALLLLSSSSOOOO
- the _a_t_t_r_s manpage, the _T_h_r_e_a_d::_Q_u_e_u_e manpage, the
- _T_h_r_e_a_d::_S_e_m_a_p_h_o_r_e manpage, the _T_h_r_e_a_d::_S_p_e_c_i_f_i_c manpage.
-
-
-
-
-
-
-
- Page 3 (printed 10/23/98)
-
-
-
-
-
-
- TTTThhhhrrrreeeeaaaadddd((((3333)))) 22223333////JJJJuuuullll////99998888 ((((ppppeeeerrrrllll 5555....000000005555,,,, ppppaaaattttcccchhhh 00002222)))) TTTThhhhrrrreeeeaaaadddd((((3333))))
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Page 4 (printed 10/23/98)
-
-
-
-
-
-
-